package com.chuangsi.common.utils;

import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.Random;
import java.util.UUID;

public class MyStringUtils {


    /**
     * 获取6位随机验证码
     *
     * @return
     */
    public static String getRandomCode() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        do {
            code.append(random.nextInt(10));
        } while (code.length() < 6);
        return code.toString();
    }


    /**
     * 生成12位的随机数
     */

    public static Long number12(){
        long number12 = (long) (Math.random() * (999999999999L - 100000000000L))+100000000000L;
        return number12;
    }

    public static String toSqlString(String str){
        String line = "";
        char[] arr = str.toCharArray();
        for(int i = 0;i <arr.length;i++){
            if(arr[i] >='A' && arr[i] <= 'Z'){
                line += "_"+(char)(arr[i]+32);
            }else{
                line += arr[i];
            }
        }
        return line;

    }

    /**
     * 对多选选项进行排序 如 ZDAB -> ZABD, 如果不是以Z开头，原样返回
     * @param checkboxOptionsStr
     * @return
     */
    public static String sortCheckboxOptions(String checkboxOptionsStr) {
        if(!checkboxOptionsStr.startsWith("Z")) return checkboxOptionsStr;
        char[] str;
        char temp;
        String string = checkboxOptionsStr.substring(1);
        str = string.toCharArray();
        for(int i=0;i<string.length();i++) {
            for(int j=string.length()-1;j>i;j--)
            {
                if(str[j]<=str[j-1])
                {
                    temp=str[j];
                    str[j]=str[j-1];
                    str[j-1]=temp;
                }
            }
        }
        return "Z" + new String(str);
    }

    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++)
        {
            if (!Character.isDigit(str.charAt(i)))
            {
                return false;
            }
        }
        return true;
    }

    /**
     * 生成uuid
     *
     * @return
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }

    /**
     * 生成16位id
     * @return
     */
    public static String generatePrimaryKey() {
        return UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 10) + new StringBuffer(Long.toHexString(System.currentTimeMillis()))
                .reverse().toString().substring(0, 6);
    }

    /**
     * 判断字符是否为空，和是否在对定长度内
     *
     * @param str
     *            字符�?
     * @param minLen
     *            �?小长�?
     * @param maxLen
     *            �?大长�?
     * @return true表示合法，false表示不合�?
     */
    public static boolean checkLen(String str, int minLen, int maxLen) {
        boolean isOk = true;
        if (str == null || str.length() < minLen || str.length() > maxLen) {
            isOk = false;
        }
        return isOk;
    }

    /**
     *
     * 生成加密�?
     *
     * @param str
     *            原始字符�?
     * @return 加密字符�?
     */
    public static String getEncodeStr(String str) {
        String encodeStr = str;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            encodeStr = Base64.getEncoder().encodeToString(md5.digest(str.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return encodeStr;
    }
    /**
     * 两次Md5加密
     * @param userPass
     * @param pwdkey
     * @return
     */
    public static String twiceMd5Encode(String userPass, String pwdkey) {
        String str = ""+pwdkey+userPass;
        try {
            str = DigestUtils.md5DigestAsHex(DigestUtils.md5DigestAsHex(str.getBytes("utf-8")).getBytes("utf-8"));
            str = "###" + str;
        } catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 判断是否为空
     * @param cs
     * @return
     */
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 判断是否不为空
     * @param cs
     * @return
     */
    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }


    /**
     * 获取当前时间戳
     * @return
     */
    public static Integer getTimeMillis(){
        Long timeMillis = System.currentTimeMillis();
        return timeMillis.intValue();
    }
}
